/*
 * Copyright 2008 Stuart Moffatt stuartmoffatt@gmail.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.googlecode.emcode.rvha.client.ui;

import com.google.gwt.maps.client.MapPane;
import com.google.gwt.maps.client.MapPaneType;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.geom.LatLngBounds;
import com.google.gwt.maps.client.geom.Point;
import com.google.gwt.maps.client.overlay.Overlay;
import com.google.gwt.user.client.ui.Image;

public class LegendOverlay extends Overlay {

    private MapWidget map;
    private MapPane pane;
    
    private Image legend;
    private LatLngBounds bounds;
    
    private int top;
    private int left;
    
    public LegendOverlay(String imageURL, int left, int top)
    {
        legend = new Image(imageURL);
        this.top = top;
        this.left = left;
    }
    
    @Override
    protected Overlay copy() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected void initialize(MapWidget map) {
        this.map = map;
        this.bounds = map.getBounds();
        pane = map.getPane(MapPaneType.FLOAT_PANE);
        pane.add(legend);
        pane.setStyleName("legend");
        
        //TODO set position of widget in pane
        pane.setWidgetPosition(legend, left, top);
    }

    @Override
    protected void redraw(boolean force) {
        
        bounds = map.getBounds();
        Point sw = map.convertLatLngToDivPixel(bounds.getSouthWest());
        Point ne = map.convertLatLngToDivPixel(bounds.getNorthEast());
        
        //
        pane.setWidgetPosition(legend, 
                Math.min(sw.getX(), ne.getX()) + left,
                Math.min(sw.getY(), ne.getY()) + top);
        //
        //pane.setWidgetPosition(legend, left, top);
        
    }

    @Override
    protected void remove() {
        legend.removeFromParent();
    }

}
